package com.qf.controller;

import cn.dsna.util.images.ValidateCode;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

@WebServlet("/code")
public class CodeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        ValidateCode validateCode = new ValidateCode(120, 40, 1, 1);
        String code = validateCode.getCode();
        request.getSession().setAttribute("code", code);
        System.err.println("验证码是"+code);
        BufferedImage buffImg = validateCode.getBuffImg();
        ImageIO.write(buffImg, "jpeg", response.getOutputStream());
    }
}
